package pojo.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.sun.org.apache.regexp.internal.RESyntaxException;

import pojo.Book;
import pojo.Order;
import pojo.User;
import pojo.dao.PojoDao;

public class PojoDaoImpl implements PojoDao {

	@Override
	public boolean findUser(User user, Connection connection) {
		// TODO Auto-generated method stub
		boolean result=false;
		String sqlString="select psd from busers where username=?";
		PreparedStatement pst=null;
		ResultSet rSet=null;
		try {
			pst=connection.prepareStatement(sqlString);
			pst.setString(1, user.getUsername());
			rSet=pst.executeQuery();
			while(rSet.next())
			{
				if(rSet.getString("psd").equals(user.getPassword()))
				{
					result=true;
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public List<Book> findBooks(Connection connection) {
		// TODO Auto-generated method stub
		String sqlString="select * from books";
		List<Book> bookList=new ArrayList<Book>();
		PreparedStatement pst=null;
		ResultSet rSet=null;
		try {
			pst=connection.prepareStatement(sqlString);
			rSet=pst.executeQuery();
			while(rSet.next())
			{
				Book tempBook=new Book();
				tempBook.setId(rSet.getString("id"));
				tempBook.setBookname(rSet.getString("bookname"));
				tempBook.setBookpath(rSet.getString("bookpath"));
				bookList.add(tempBook);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bookList;
	}

	@Override
	public int addOrder(Order order, Connection connection) {
		// TODO Auto-generated method stub
		int m=0;
		String sqlString="insert into border(id,userId,bookId,buynum,buydate) values(?,?,?,?,?)";
		PreparedStatement pst=null;
		try {
			pst=connection.prepareStatement(sqlString);
			pst.setString(1, UUID.randomUUID().toString());
			pst.setString(2, order.getUserId());
			pst.setString(3, order.getBookId());
			pst.setInt(4, order.getCount());
			pst.setDate(5, new java.sql.Date(order.getDate().getTime()));
			m=pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return m;
	}
	public  String getUserId( User user, Connection connection) {
		// TODO Auto-generated method stub
		int m=0;
		String sqlString="select id from busers where username=?";
		PreparedStatement pst=null;
		ResultSet rSet=null;
		try {
			pst=connection.prepareStatement(sqlString);
			pst.setString(1,user.getUsername());
			rSet=pst.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String id="";
		try {
			while(rSet.next())
			{
				id=rSet.getString("id");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return id;
	}
	
	
}
